a<template>
  <div>
    <a-card :bordered="false" :bodyStyle="tstyle">
      <div class="table-page-search-wrapper" v-if="hasPerm('biOperatedData:page')">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="8" :sm="24">
              <a-form-item label="名称">
                <a-input v-model="queryParam.storeName" allow-clear placeholder="请输入名称" />
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="数据来源">
                <a-select v-model="queryParam.orgCode" placeholder="请选择数据来源" allow-clear>
                  <a-select-option v-for="item in orgList" :key="item.code" :value="item.code">{{ item.name }}
                  </a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <template v-if="advanced">
              <a-col :md="8" :sm="24">
                <a-form-item label="店号">
                  <a-input v-model="queryParam.storeId" allow-clear placeholder="请输入店号" />
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="电话号码">
                  <a-input v-model="queryParam.phoneNumber" allow-clear placeholder="请输入电话号码" />
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="省份">
                  <a-input v-model="queryParam.provName" allow-clear placeholder="请输入省份" />
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="城市">
                  <a-input v-model="queryParam.cityName" allow-clear placeholder="请输入城市" />
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="区/县">
                  <a-input v-model="queryParam.countryName" allow-clear placeholder="请输入区/县" />
                </a-form-item>
              </a-col>
            </template>
            <a-col :md="8" :sm="24">
              <span class="table-page-search-submitButtons">
                <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                <a-button style="margin-left: 8px" @click="() => (queryParam = {})">重置</a-button>
                <a @click="toggleAdvanced" style="margin-left: 8px">
                  {{ advanced ? '收起' : '展开' }}
                  <a-icon :type="advanced ? 'up' : 'down'" />
                </a>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card :bordered="false">
      <s-table
        ref="table"
        :columns="columns"
        :data="loadData"
        :alert="options.alert"
        :rowKey="(record) => record.id"
        :rowSelection="options.rowSelection"
      >
        <template class="table-operator" slot="operator" v-if="hasPerm('biOperatedData:add')">
          <a-button type="primary" v-if="hasPerm('biOperatedData:add')" icon="plus" @click="$refs.addForm.add()">新增</a-button
          >
          <!-- <a-button
            type="danger"
            :disabled="selectedRowKeys.length < 1"
            v-if="hasPerm('biOperatedData:delete')"
            @click="batchDelete"><a-icon type="delete" />批量删除</a-button> -->
          <x-down v-if="hasPerm('biOperatedData:export')" ref="batchExport" @batchExport="batchExport" />
        </template>
        <span slot="action" slot-scope="text, record">
          <a :href="getMapUrl(record)" target="_blank">地图</a>
          <a-divider type="vertical" />
          <a v-if="hasPerm('biOperatedData:edit')" @click="findCouponList(record), (visible = true)">核销券列表</a>
          <a-divider type="vertical" v-if="hasPerm('biOperatedData:edit') & hasPerm('biOperatedData:delete')" />
          <!-- <a-popconfirm
            v-if="hasPerm('biOperatedData:delete')"
            placement="topRight"
            title="确认删除？"
            @confirm="() => singleDelete(record)"
          >
            <a>删除</a>
          </a-popconfirm> -->
        </span>
        <span slot="orgCodeSlot" slot-scope="text">
          {{ getOrgName(text) }}
        </span>
      </s-table>
      <add-form ref="addForm" :orgList="orgList" @ok="handleOk" />
      <edit-form ref="editForm" @ok="handleOk" />
    </a-card>
    <a-modal
      :width="1200"
      :heigh="200"
      title="数据列表"
      :visible="visible"
      @ok="handleOk"
      @cancel="handleCancel"
      :footer="null"
    >
      <a-table
        :scroll="scroll"
        size="small"
        :dropdown-style="{ maxHeight: '200px', overflow: 'auto' }"
        ref="table"
        :columns="Couponcolumns"
        :data-source="couponList"
      >
        <span slot="timeValue" slot-scope="text, record">
          <span> {{ record.day != '' ? record.day : record.month == '' ? record.month : record.year }} </span>
          <!-- <span> {{record}}</span> -->
        </span>
      </a-table>
    </a-modal>
  </div>
</template>
<script>
import { STable, XDown } from '@/components'
import {
  biOperatedDataPage,
  biOperatedDataDelete,
  biOperatedDataExport,
} from '@/api/modular/main/bioperateddata/biOperatedDataManage'
import { biCouponWriteList } from '@/api/modular/main/bicouponwrite/biCouponWriteManage'
import addForm from './addForm.vue'
import editForm from './editForm.vue'
import { getOrgList } from '@/api/modular/system/orgManage'
import _ from 'lodash'

export default {
  components: {
    STable,
    addForm,
    editForm,
    XDown,
  },
  data() {
    return {
      scroll: {
        y: 280,
      },
      visible: false,
      // 对应的核销数据list
      couponList: [],
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: {},
      // 核销表头
      Couponcolumns: [
        {
          title: '店号',
          align: 'center',
          dataIndex: 'writeStoreId',
        },
        {
          title: '活动类型',
          align: 'center',
          dataIndex: 'activityType',
        },
        {
          title: '活动名称',
          align: 'center',
          dataIndex: 'activityName',
        },
        {
          title: '卷名称',
          align: 'center',
          dataIndex: 'couponName',
        },
        {
          title: '券核销面额(元)',
          align: 'center',
          dataIndex: 'orderAmount',
        },
      ],
      // 表头
      columns: [
        {
          title: '店号',
          align: 'center',
          dataIndex: 'storeId',
        },
        {
          title: '名称',
          align: 'center',
          dataIndex: 'storeName',
        },
        {
          title: '省份',
          align: 'center',
          dataIndex: 'provName',
        },
        {
          title: '城市',
          align: 'center',
          dataIndex: 'cityName',
        },
        {
          title: '详细地址',
          align: 'center',
          dataIndex: 'address',
          ellipsis: true,
        },
        {
          title: '数据来源',
          align: 'center',
          dataIndex: 'orgCode',
          scopedSlots: { customRender: 'orgCodeSlot' },
        },
      ],
      tstyle: { 'padding-bottom': '0px', 'margin-bottom': '10px' },
      // 加载数据方法 必须为 Promise 对象
      loadData: (parameter) => {
        return biOperatedDataPage(Object.assign(parameter, this.queryParam)).then((res) => {
          return res.data
        })
      },
      selectedRowKeys: [],
      selectedRows: [],
      options: {
        alert: {
          show: true,
          clear: () => {
            this.selectedRowKeys = []
          },
        },
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys,
          onChange: this.onSelectChange,
        },
      },
      orgParams: {},
      orgList: [],
    }
  },
  created() {
    if (this.hasPerm('biOperatedData:edit') || this.hasPerm('biOperatedData:delete')) {
      this.columns.push({
        title: '操作',
        width: '150px',
        dataIndex: 'action',
        scopedSlots: { customRender: 'action' },
      })
    }
    this.findOrgList()
  },
  methods: {
    // 核销列表
    findCouponList(record) {
      this.queryParam.writeStoreId = record.storeId
      biCouponWriteList(this.queryParam).then((res) => {
        this.couponList = res.data
        console.info(res.data)
      })
    },
    // 组织列表
    findOrgList() {
      getOrgList(this.orgParams).then((res) => {
        this.orgList = res.data
      })
    },
    getOrgName(code) {
      const obj = _.find(this.orgList, { code: code })
      if (obj) {
        return obj.name
      }
      return code
    },
    getMapUrl(record) {
      return 'https://uri.amap.com/marker?position=' + record.lng + ',' + record.lat
    },
    /**
     * 单个删除
     */
    singleDelete(record) {
      const param = [{ id: record.id }]
      this.biOperatedDataDelete(param)
    },
    /**
     * 批量删除
     */
    batchDelete() {
      const paramIds = this.selectedRowKeys.map((d) => {
        return { id: d }
      })
      this.biOperatedDataDelete(paramIds)
    },
    biOperatedDataDelete(record) {
      biOperatedDataDelete(record).then((res) => {
        if (res.success) {
          this.$message.success('删除成功')
          this.$refs.table.clearRefreshSelected()
        } else {
          this.$message.error('删除失败') // + res.message
        }
      })
    },
    toggleAdvanced() {
      this.advanced = !this.advanced
    },
    /**
     * 批量导出
     */
    batchExport() {
      const paramIds = this.selectedRowKeys.map((d) => {
        return { id: d }
      })
      biOperatedDataExport(paramIds).then((res) => {
        this.$refs.batchExport.downloadfile(res)
      })
    },
    handleOk() {
      this.$refs.table.refresh()
    },
    handleCancel(e) {
      console.log('Clicked cancel button')
      this.visible = false
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
  },
}
</script>
<style lang="less">
.table-operator {
  margin-bottom: 18px;
}
button {
  margin-right: 8px;
}
</style>
